package com.sm.honkon.ui.fragment.signuse

import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.lifecycle.observe
import androidx.recyclerview.widget.RecyclerView
import com.sm.honkon.R
import com.sm.honkon.app.base.BaseFragment
import com.sm.honkon.app.ext.goBackHome
import com.sm.honkon.app.ext.goMannger
import com.sm.honkon.data.bean.HandlerEntity
import com.sm.honkon.databinding.FrgAddHandelBinding
import com.sm.honkon.ui.adapter.PopWindowAdapter
import com.sm.honkon.ui.model.AddHandlerViewModel
import kotlinx.android.synthetic.main.frg_add_handel.*
import me.hgj.jetpackmvvm.ext.nav

class AddHandleFragment : BaseFragment<AddHandlerViewModel, FrgAddHandelBinding>() {
    override fun layoutId(): Int = R.layout.frg_add_handel


    val mPopWindow: PopupWindow by lazy {
        val contentView: View = LayoutInflater.from(requireContext())
            .inflate(R.layout.popwindow, null)
        PopupWindow(
            contentView,
            245, ViewGroup.LayoutParams.WRAP_CONTENT, true
        ).apply {
            this.contentView = contentView
            val recyclerView = contentView.findViewById<RecyclerView>(R.id.host_recycle)
            recyclerView.adapter = adapter

            //解决5.0以下版本点击外部不消失问题
            isOutsideTouchable = true
            setBackgroundDrawable(BitmapDrawable())
        }

    }
    val adapter by lazy {
        PopWindowAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                if (data == mViewModel.medicalOfficials) {
                    et_doctor_title.text = data[position]
                    mViewModel.handle.value?.medical_official = data[position]
                } else {
                    et_status.text = data[position]
                    mViewModel.handle.value?.status = position
                }
                mPopWindow.dismiss()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.getParcelable<HandlerEntity>("handler")?.let {
            mViewModel.handle.value = it
            et_doctor_title.text = it.medical_official
            et_status.text = mViewModel.status[it.status]
            mViewModel.isEdit.set(true)
        }
        mDatabind.model = mViewModel
        mDatabind.click = Click()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.isMan.observe(this) {
            mViewModel.handle.value?.sex = if (it) 0 else 1
        }
    }

    inner class Click {
        fun save() {
            mViewModel.insert {
                back()
            }
        }

        fun reset() {
            mViewModel.handle.postValue(HandlerEntity(mViewModel.uid))
            et_doctor_title.text = ""
            et_status.text = "正常"
        }

        fun back() {
            nav().navigateUp()
        }

        fun home() {
            nav().goBackHome()
        }

        fun mannger() {
            nav().goMannger()
        }

        fun exit() {
            System.exit(0)
        }

        fun showMedicalOfficial(view: View) {
            adapter.setNewInstance(mViewModel.medicalOfficials)
            mPopWindow.showAsDropDown(view)
        }

        fun showStatus(view: View) {
            adapter.setNewInstance(mViewModel.status)
            mPopWindow.showAsDropDown(view)
        }

        fun setting() {
            nav().navigate(R.id.settingFragment)
        }
    }
}